package core.views;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;


import java.awt.event.*;
import java.util.Random;
import core.Runner;
import core.models.Places;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import core.models.Paces;
import core.models.Rations;
import core.models.Save;

public class TrailPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField distField;
	private JTextField distNumField;
	private JTextField txtFoodRemaining;
	private JTextField foodRemNumField;
	private Runner runner;
	private mainButtonListener listener;
	private invButtonListener invListener;
	private saveButtonListener saveListener;
	private JTextField txtDistanceToStore;
	private JTextField distToStore;
	private JTextField distToEndText;
	private JTextField distToEnd;
	private JTextField paceField;
	private JTextField rationField;
	private JComboBox paceComboBox;
	private JComboBox foodRationsComboBox;
	private JButton mapButton;
	private MapPanel panel;
	private JFrame mapFrame = new JFrame();
	private JButton turnBtn;
	private JButton invButton;
	private JButton saveButton;

	public TrailPanel(Runner runner) {
		setLayout(null);
		this.runner = runner;
		panel = new MapPanel(runner);
		mapFrame.getContentPane().add(panel);
		mapFrame.pack();
		
		distField = new JTextField();
		distField.setEditable(false);
		distField.setText("Distance Traveled:");
		distField.setBounds(10, 11, 104, 20);
		add(distField);
		distField.setColumns(10);
		
		distNumField = new JTextField();
		distNumField.setEditable(false);
		distNumField.setBounds(129, 11, 49, 20);
		distNumField.setText("" + runner.getGroup().getDistance() + ""	);
		add(distNumField);
		distNumField.setColumns(10);
		
		txtFoodRemaining = new JTextField();
		txtFoodRemaining.setEditable(false);
		txtFoodRemaining.setText("Food Remaining:");
		txtFoodRemaining.setColumns(10);
		txtFoodRemaining.setBounds(10, 42, 104, 20);
		add(txtFoodRemaining);
		
		foodRemNumField = new JTextField();
		foodRemNumField.setEditable(false);
		foodRemNumField.setColumns(10);
		foodRemNumField.setBounds(129, 42, 49, 20);
		add(foodRemNumField);
		foodRemNumField.setText(""+runner.getGroup().getWagon().getInventory().getItemQuantity("Food"));
		System.out.println(runner.getGroup().getWagon().getInventory().getItemQuantity("Food") + "food");
		
		turnBtn = new JButton("Take a Turn");
		turnBtn.setBounds(10, 266, 104, 34);
		add(turnBtn);
		
		txtDistanceToStore = new JTextField();
		txtDistanceToStore.setText("Distance to store:");
		txtDistanceToStore.setEditable(false);
		txtDistanceToStore.setBounds(216, 11, 104, 20);
		add(txtDistanceToStore);
		txtDistanceToStore.setColumns(10);
		
		distToStore = new JTextField();
		distToStore.setEditable(false);
		distToStore.setBounds(330, 11, 86, 20);
		add(distToStore);
		distToStore.setColumns(10);
		
		distToEndText = new JTextField();
		distToEndText.setEditable(false);
		distToEndText.setText("Distance remaining:");
		distToEndText.setBounds(216, 42, 104, 20);
		add(distToEndText);
		distToEndText.setColumns(10);
		
		distToEnd = new JTextField();
		distToEnd.setEditable(false);
		distToEnd.setBounds(330, 42, 86, 20);
		add(distToEnd);
		distToEnd.setColumns(10);
		
		paceField = new JTextField();
		paceField.setText("Pace:");
		paceField.setEditable(false);
		paceField.setColumns(10);
		paceField.setBounds(10, 73, 104, 20);
		add(paceField);
		
		rationField = new JTextField();
		rationField.setText("Rations:");
		rationField.setEditable(false);
		rationField.setColumns(10);
		rationField.setBounds(10, 104, 104, 20);
		add(rationField);
		
		paceComboBox = new JComboBox();
		paceComboBox.setModel(new DefaultComboBoxModel(Paces.values()));
		paceComboBox.setBounds(129, 73, 85, 20);
		add(paceComboBox);
		
		foodRationsComboBox = new JComboBox();
		foodRationsComboBox.setModel(new DefaultComboBoxModel(Rations.values()));
		foodRationsComboBox.setBounds(129, 104, 85, 20);
		add(foodRationsComboBox);
		
		mapButton = new JButton("Show map");
		mapButton.setBounds(224, 266, 96, 34);
		add(mapButton);
		
		invButton = new JButton("Inventory");
		invButton.setBounds(122, 266, 92, 34);
		add(invButton);
		invListener = new invButtonListener();
		invButton.addActionListener(invListener);
		
		saveButton = new JButton("Save Game");
		saveButton.setBounds(330, 266, 103, 34);
		add(saveButton);
		saveListener = new saveButtonListener();
		saveButton.addActionListener(saveListener);
		
		mapButtonListener listener2 = new mapButtonListener();
		mapButton.addActionListener(listener2);
		listener = new mainButtonListener();
		turnBtn.addActionListener(listener);
		
	}
	
	public void loadData() {
		distNumField.setText("" + runner.getGroup().getDistance());
		foodRemNumField.setText(""+runner.getGroup().getWagon().getInventory().getItemQuantity("Food"));
		distToStore.setText("" + runner.getGroup().toNextStore());
		distToEnd.setText("" + (1500 - runner.getGroup().getDistance()));
		foodRationsComboBox.setSelectedItem(runner.getGroup().getRation());
		paceComboBox.setSelectedItem(runner.getGroup().getWagon().getPace());

	}
	
	
	/**
	 * Updates the pace and rations for the group.
	 */
	public void update() {
		runner.getGroup().getWagon().setPace((Paces) paceComboBox.getSelectedItem());
		runner.getGroup().setRation((Rations) foodRationsComboBox.getSelectedItem());
		System.out.println(runner.getGroup());
		System.out.println(runner.getGroup().getWagon());
	}
	
	private class mainButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			//loadData();

			update();
			loadData();
			runner.getGroup().moveWagon();
			runner.getGroup().getRoad().updatePlaces(runner.getGroup().getDistance());
			distNumField.setText("" + runner.getGroup().getDistance() + "");
			System.out.println(runner.getGroup().getWagon().getPace());
			System.out.println(runner.getGroup().getWagon().getInventory().getItemQuantity("Food"));
			foodRemNumField.setText(""+runner.getGroup().getWagon().getInventory().getItemQuantity("Food"));
			Places p = runner.getGroup().getRoad().getPlace();
			distToStore.setText("" + runner.getGroup().toNextStore());
			distToEnd.setText("" + (1500 - runner.getGroup().getDistance()));
			if(p.getPlace() == 1)
			{
				StartPanelMain.changeCard("Store");
				StartPanelMain.showButtons();
			}
			else if(p.getPlace() == 2)
			{
			//	JOptionPane.showMessageDialog(null, "You are at a river, Sucks to be you!");
				StartPanelMain.changeCard("River");
			}
			else if(p.getPlace() ==3)
			{
				JFrame y = new JFrame();
				JOptionPane.showMessageDialog(y, "YOU WON!!!!!!!!");
				turnBtn.setVisible(false);
			}
			
//			Update map when the take a turn button is pressed
//			Need to repaint both the frame and the panel
			panel.updateMap();
			mapFrame.repaint();
//			panel.repaint();
			throwPossibleEvent();
		}
	}
	
	private class mapButtonListener implements ActionListener
	{
		
		public void actionPerformed(ActionEvent e)
		{
			panel.updateMap();
			mapFrame.setVisible(true);
		}
	}
	
	private class invButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			StartPanelMain.changeCard("Inventory");
		}
	}
	
	private class saveButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			Save.saveToDisk("savefile.dat",runner);
		}
	}
	
	
	
	
	
	
	
	//************************************EVENTS METHODS****************************************************
	 
	
	public void throwPossibleEvent()
	{
		Random rand = new Random();
		int eventRoll =  1+ rand.nextInt(200);
		int numToBreak = 0;
		if(runner.getGroup().getWagon().getPace().getSpeed() >= 3)
			numToBreak += 10;
		else if(runner.getGroup().getWagon().getPace().getSpeed() <= 1)
			numToBreak -= (5 - (2 * runner.getGroup().getWagon().getPace().getSpeed()));
		if(runner.getGroup().getWagon().getInventory().getItemQuantity("Food") <= 0)
			numToBreak += 8;
		else if (runner.getGroup().getRation().getConsumption() <= 1)
			numToBreak += 5;
		else if (runner.getGroup().getRation().getConsumption() >= 3)
			numToBreak -= 2;
		if (runner.getGroup().getWagon().getInventory().getItemQuantity("Clothing") >= runner.getGroup().getNumPeopleAlive())
			numToBreak -=2;
		else
			numToBreak += 2;
		if (eventRoll <= numToBreak)
		{
			throwEvent();
		}
//		updatePlayerHealth();
		
	}
	
	public void updatePlayerHealth()
	{
		
		int numToAdd = 0;
		if (runner.getGroup().getWagon().getInventory().getItemQuantity("Food") <= 0)
			numToAdd -= 20;
		else if (runner.getGroup().getRation().getConsumption() >= 3)
			numToAdd += 5;
		else if (runner.getGroup().getRation().getConsumption() <= 1)
			numToAdd -= 2;
		if(runner.getGroup().getWagon().getPace().getSpeed() >= 3)
			numToAdd -= 2;
		else if(runner.getGroup().getWagon().getPace().getSpeed() <= 1)
			numToAdd += (4 - (2 * runner.getGroup().getWagon().getPace().getSpeed()));
		for (int x = 0; x < runner.getGroup().getPeople().size(); x++)
		{
			if( runner.getGroup().getPeople().get(x).getAlive())
				{
					runner.getGroup().getPeople().get(x).addToStatus(numToAdd);
					if( !(runner.getGroup().getPeople().get(x).getAlive()))
					{
						JOptionPane.showMessageDialog(null,"" + runner.getGroup().getPeople().get(x).toString() + "Has Died");
					}
				}
		}
	}
	
	public void throwEvent()
	{
		String temp="";
		Random rand = new Random();
		int eventRoll =  1+ rand.nextInt(5);
		if (eventRoll==1) {
			temp += "You have been attacked by a witch, luckily she ran out of mana... while attemping to escape: \n";
			temp += runner.getGroup().getWagon().getInventory().dropRand(.8);
		}
		else if(eventRoll==2) {
			temp += "A wild bear has attacked you... while attemping to escape: \n";
			temp += runner.getGroup().getWagon().getInventory().dropRand(.8);
			
		}
		
		else if(eventRoll==3) {
			temp += "Mother nature has been unkind. A violent storm has ravaged your camp...\n";
			temp += runner.getGroup().getWagon().getInventory().dropRand(.8);
		}
		
		else if(eventRoll==4) {
			temp += "Native Americans have attacked you. Luckily, they only had rocks and bows and you killed all of them.";
		}
		
		else if(eventRoll==5) {
			temp += "You have found $1000!";
			runner.getGroup().setMoney(runner.getGroup().getMoney() + 1000);
		}
		
		
		JOptionPane.showMessageDialog(null,temp);	
		loadData();
	}
	 
}
